<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BPartnershipDataObj
 *
 * @author I062261
 */
class BRelationshipEntity extends BEntity {

    //put your code here
    public $mainObj = null;
    public $pairObj = null;
    public $pairObjs = array();
    public $relationship = null;
    public $DataStoreObject = null;
    public $spaceId = null;
    public $appId = null;
    public $findSet = array();
    public $resultSet = array();
    public $variableName = null;

    public function __construct($mainObj = NULL, $pairObjs = array(), $data = NULL) {

        $variableName = '';

        if (isset($data['relationship']))
            $this->relationship = $data['relationship'];

        if ($mainObj != NULL) {
            $this->mainObj = $mainObj;
            if ($mainObj instanceof BEntity) {
                $variableName = get_class($mainObj);
                if ($mainObj->getId() != NULL)
                    $variableName.="_" . $mainObj->getId();
            }
        }
        if ($pairObjs != NULL && $pairObjs instanceof BEntity) {
            $this->pairObj = $pairObjs;
            $variableName.="_" . get_class($this->pairObj);
            if ($this->pairObj->getId() != NULL)
                $variableName.="_" . $this->pairObj->getId();
        }
        if ($pairObjs != NULL && is_array($pairObjs)) {
            $this->pairObjs = $pairObjs;
            foreach ($pairObjs as $pairObj) {
                if ($pairObj instanceof BEntity) {
                    $variableName.="_" . get_class($pairObj);
                    if ($pairObj->getId() != NULL)
                        $variableName.="_" . $pairObj->getId();
                }
            }
        }

        $this->variableName = $variableName;

        if (isset($data) && is_numeric($data) && $data > 0) {
            $this->load($data);
        }
    }

    /**
     * load entity by id
     * @param type $data
     */
    public function load($data = NULL) {
        if (is_numeric($data) && $data > 0) {
            BRepositoryFactory::createRepository()->loadRelationshipEntity($this, $data);
        }
    }

    /**
     * 
     * @param type $mainObj
     * @param BEntity $pairObjs
     * @param type $context
     * @param type $updateItems, if set updateItems, $context will be treat as search condition
     * otherwise, create new item and use context as value for new item
     * @return boolean
     */
    public function save($context = null) {
        return BRepositoryFactory::createRepository()->saveRelationshipEntity($this, $context);
    }

    public static function delete($context) {

        $realClass = null;

        if ($context instanceof BRelationshipEntity) {
            $context = array();
            $entity = $context;
            $realClass = $entity->getRealEntityClass();
            $context['id'] = $entity->getId();
        }

        if (isset($context['class']))
            $realClass = $context['class'];

        if (isset($context['entityClass']))
            $realClass = $context['entityClass'];

        if ($realClass != NULL && isset($context['id'])) {
            BRepositoryFactory::createRepository()->deletEntity($context, $realClass);
        }

        return FALSE;
    }

    public function getRealEntityClass($context = null) {

        $mainObj = NULL;
        $pairObj = NULL;
        $pairObjs = array();

        if ($this->mainObj instanceof BEntity)
            $mainObj = $this->mainObj;

        if (isset($this->pairObj) && $this->pairObj instanceof BEntity)
            $pairObj = $this->pairObj;

        if (isset($this->pairObjs) && is_array($this->pairObj)) {
            $pairObjs = $this->pairObjs;
        }

        if (isset($context[0]) && $context[0] instanceof BEntity)
            $mainObj = $context[0];
        if (isset($context[1]) && $context[1] instanceof BEntity)
            $pairObj = $context[1];
        if (isset($context[1]) && is_array($context[1])) {
            $pairObjs = $context[1];
        }

        if (isset($context['mainObj']))
            $mainObj = $context['mainObj'];

        if (isset($context['pairObj']))
            $pairObj = $context['pairObj'];

        if (isset($context['pairObjs']))
            $pairObjs = $context['pairObjs'];

        if (isset($mainObj))
            $class = get_class($this->mainObj);
        else {
            return FALSE;
        }

        if (isset($pairObj)) {
            $class .= substr(get_class($pairObj), 1);
        }

        if (isset($pairObjs)) {
            foreach ($pairObjs as $pairObj) {
                $class .= substr(get_class($pairObj), 1);
            }
        }

        return $class;
    }

    /**
     * this function use to find relationshipentity
     * @param null $mainObj
     * @param array $pairObjs
     * @param type $context
     * @return \BRelationshipEntity
     */
    public static function find($context = NULL) {

        $rEntities = self::findAll($context);

        return reset($rEntities);
    }

    public static function findAll($context = NULL) {

        $mainObj = isset($context['mainObj']) ? $context['mainObj'] : NULL;
        $pairObjs = isset($context['pairObjs']) ? $context['pairObjs'] : NULL;
        $relationship = isset($context['relationship']) ? $context['relationship'] : NULL;

        if ($mainObj == null || $pairObjs == NULL)
            return FALSE;

        $entity = self::build($mainObj, $pairObjs, array('relationship' => $relationship));

        //because relationshipEntity is a time-costing entity, save it into instance for performance
        $results = Blyn::app()->getInstanceVariable($entity->variableName);

        if ($results != NULL)
            return $results;
        else {
            $results = BRepositoryFactory::createRepository()->findAllRelationshipEntity($entity, $context);
            Blyn::app()->addInstanceVariable($entity->variableName, $results);
            return $results;
        }
    }

    public static function build($mainObj, $pairObjs, $context = NULL) {
        return new BRelationshipEntity($mainObj, $pairObjs, $context);
    }

}

?>
